﻿$(function(){//延迟加载
	var dvbelong='';
	var done=0;
	var uprows=null;
	var newrows=null;
	var deleterows=null;
	var editIndex = undefined;	
	
	$('#searchres').datagrid({
		width:'auto',
		height:'auto',
		//striped:true,	
//		toolbar:'#tb1',
		url:'showallroles',
		method:'get',
		pagination:true,
		rownumbers:true,
		pageSize:'20',
//		pageList:{20,30},
		toolbar: [{
			iconCls: 'icon-add',
			text:'职位新增',
			handler: function(){
				append();
				}
		},'-',{
			iconCls: 'icon-save',
			text:'确认修改',
			handler: function(){
				accept();
			}
		},'-',{
			iconCls: 'icon-undo',
			text:'取消修改',
			handler: function(){
				reject();
			}
		},'-',{
			iconCls: 'icon-remove',
			text:'删除职位',
			handler: function(){
				removeit();
			}
		}],
		columns:
		[[
		{"field":"rolename",
		"title":"职位名称",
		"width":"20%",
		editor:{
      	  type:'textbox',
      		 options:{		        			 
      			 required:true 
      		 }
        }
		},
		{
		"field":"roletype",
		"title":"职位类型",
		"width":"20%",
		editor:{
	      	  type:'textbox',
	      		 options:{		        			 
	      			 required:true 
	      		 }
	        }
		},
		{
		"field":"rolelevel",
		"title":"职位级别（0最高）",
		"width":"20%",
		editor:{
      	  type:'numberbox',
      		 options:{		        			 
      			 required:true 
      		 }
        }
		},
		{
		"field":"roledescribe",
		"title":"职位描述",
		"width":"30%",
		editor:{
      	  type:'textbox',
        }
		}
		]],
		onDblClickRow:onClickRow
	});
	
	$('#compledit').click(function(){
		if(done==0){
			alert('请先确定修改');
			return;
		}
		if(uprows.length==0&&newrows.length==0&&deleterows.length==0){
			alert('未修改');
			return;
		}
		var listupdate=JSON.stringify(uprows);
		var listnew=JSON.stringify(newrows);
		var listdelete=JSON.stringify(deleterows);
		$.ajax({
			url:'updateroles',
			type:'POST',
			data:{
				listupdate:listupdate,
				listnew:listnew,
				listdelete:listdelete
			},
			success:function(data){
				alert(data.msg);
				$('#searchres').datagrid('reload');
			}
		});
	});
	
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#searchres').datagrid('validateRow', editIndex)){
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}

	function onClickRow(index){
		done=0;
		if (editIndex!= index){
			if (endEditing()){
				$('#searchres').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#searchres').datagrid('selectRow', editIndex);
			}
		}
	}

	function append(){
		if (endEditing()){
			$('#searchres').datagrid('appendRow',{
				rolename:'',
				roletype:'',
				rolelevel:10,
				roledescribe:''
			});

			editIndex = $('#searchres').datagrid('getRows').length-1;
			$('#searchres').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
			}
		done=0;
		}
	
	function removeit(){
		if (editIndex == undefined){return}
		$('#searchres').datagrid('cancelEdit', editIndex).datagrid('deleteRow', editIndex);

	}

	function accept(){
		if (endEditing()){
			done=1;
			uprows = $('#searchres').datagrid('getChanges','updated');
			newrows = $('#searchres').datagrid('getChanges','inserted');
			deleterows=$('#searchres').datagrid('getChanges','deleted');
			$('#searchres').datagrid('acceptChanges');
			editIndex = undefined;
		}
	}

	function reject(){
		$('#searchres').datagrid('rejectChanges');
		editIndex = undefined;
	}

	function getChanges(){
		var rows = $('#searchres').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}
});